﻿using Alos.Entity;
using Alos.IRepository;
using PetaPoco;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Alos.Repository
{
    public class ReTeachRecordRepository : BaseRepository<ReTeachRecord>, IReTeachRecordRepository
    {
        /// <summary>
        /// 添加教师上课记录
        /// </summary>
        public int CreateReTeachRecord(ReTeachRecord ReTeachRecord)
        {            
            ReTeachRecord.Status = 0;
            ReTeachRecord.StartTime = DateTime.Now;
            ReTeachRecord.EndTime = null;
            ReTeachRecord.InputTime = DateTime.Now;
            return Convert.ToInt32(ReTeachRecord.Insert());
        }
        /// <summary>
        /// 修改教师上课记录
        /// </summary>
        public int UpdateReTeachRecord(int id)
        {
            var sql = Sql.Builder.Append(" SET [Status]=1 WHERE @0 IN( SELECT B.ID FROM [dbo].[ReTeachRecord] B WHERE B.ID NOT IN( SELECT TOP 1 A.ID  FROM [dbo].[ReTeachRecord] A ORDER BY A.StartTime DESC))", id);
            return DBContext.Update<ReTeachRecord>(sql);
        }
        /// <summary>
        /// 检查上一堂课是否正常结束
        /// </summary>
        public ReTeachRecord getReTeachRecord(int TeacherId, int ClassId)
        {
            var sql = Sql.Builder.Append("SELECT TOP 1 * FROM ReTeachRecord A  WHERE A.TeacherID=@0 AND A.ClassID=@1 AND A.[Status]=0 ORDER BY A.InputTime DESC", TeacherId, ClassId);
            return DBContext.SingleOrDefault<ReTeachRecord>(sql);
           
        }
        /// <summary>
        /// 获取最近一条上课记录
        /// </summary>
        /// <returns>ReTeachRecord</returns>
        public ReTeachRecord GetReTeachRecord()
        {
            var sql = "SELECT TOP 1 * FROM dbo.ReTeachRecord A ORDER BY A.StartTime DESC";
            return DBContext.Single<ReTeachRecord>(sql);
        }
        /// <summary>
        /// 点击下课
        /// </summary>
        public IEnumerable<dynamic> EndReTeachRecord(int TeacherID, int LessionID)
        {
            var sql = Sql.Builder.Append("UPDATE ReTeachRecord SET [Status]=1 WHERE TeacherID=@0 AND LessionID=@1 UPDATE RePutQuestion SET [IsEnd]=1 WHERE TeacherID=@0 AND LessionID=@1 UPDATE ReVote SET [IsEnd]=1 WHERE TeacherID=@0 AND LessionID=@1 UPDATE ReExam SET [IsEnd]=1 WHERE TeacherID=@0 AND LessionID=@1 UPDATE ReActivity SET [IsEnd]=1 WHERE TeacherID=@0 AND LessionID=@1 UPDATE ReArrangeRecord SET [Status]=3 WHERE UserID=@0 AND LessonID=@1 AND [Status]=2", TeacherID, LessionID);
            return DBContext.Query<dynamic>(sql);

        }
        /// <summary>
        /// 学生登录后  等待老师准备上课
        /// </summary>
        /// <param name="classId">班级编号</param>
        /// <returns></returns>
        public dynamic isBeginTeach(int classId)
        {
            var _teachRecord = DBContext.SingleOrDefault<dynamic>("SELECT TOP 1  * FROM dbo.ReTeachRecord A where ClassID=@0 ORDER BY A.StartTime DESC", classId);
            dynamic result = null;
            if (_teachRecord!=null && _teachRecord.Status == 0)
            {
                result = DBContext.SingleOrDefault<dynamic>("SELECT A.Head,A.UserName,A.ID,A.UserCode,A.SubjectName FROM dbo.SysUserInfo A WHERE A.ID=@0", _teachRecord.TeacherID);
                result.Lession = DBContext.SingleOrDefault<dynamic>(@"SELECT A.LessonTitle,B.PointName,F.SubjectName,D.[Version],D.SubVersion,CASE D.UpOrDown WHEN 1 THEN '上册' WHEN 2 THEN '下册' END AS UpOrDown FROM ReLession A
                                                        INNER JOIN dbo.Chapter B ON A.ChapterID=B.ID
                                                        INNER JOIN TeachingMaterial D ON D.ID=B.TeachingMaterialID
                                                        LEFT JOIN [dbo].[DicSubject] F ON F.SubjectCode=D.SubjectCode
                                                        WHERE A.ID=@0", _teachRecord.LessionID);
                result.UserNum = DBContext.SingleOrDefault<dynamic>(@"SELECT COUNT(*) as UserNum FROM dbo.SysUserInfo WHERE RoleId=3 and ClassID=@0", classId);
            }
            return result;
        }
        public int EndPreTeachRecord(int ID)
        {
            var sql = Sql.Builder.Append(" SET [Status]=1 WHERE ID=@0", ID);
            return DBContext.Update<ReTeachRecord>(sql); ;
        }
    }
}
